<layout name="layout" />
<style type="text/css">
    .quantity .add, .quantity .subtract {
        cursor: pointer
    }
</style>
<section class="wrapper marc order order_list">
    <header>
        <figcaption>订购</figcaption>
        <nav>
            <getData model="CaiType" field="*" name="cai_type" id="row" limit="5" where_var="'type_shop_id = ' . $shop_id" order="type_id DESC"> <a href="javascript:void(0);" class="cai_type_select" type_id="{$row.type_id}">{$row.type_name}</a> </getData>
        </nav>
    </header>
    <form action="{:U('Order/index')}" method="post" id="form">
        <div>
            <volist name="list" id="list_value">
                <ul class="order_list cai_type_list cai_type_{$list_value['cai_type_id']}">
                    <li class="img"><a href="{:U('/cai/'.$list_value['cai_id'])}" target="_blank"><img src="{$list_value.cai_thumb|default='__PUBLIC_DC__/skin/img/order_list_img.jpg'}" width="171" height="111" alt="" /></a></li>
                    <li class="name"><a href="{:U('/cai/'.$list_value['cai_id'])}" target="_blank">{$list_value.cai_name}</a> [剩余:{$list_value['cai_num']}]</li>
                    <li class="quantity" cai_id="{$list_value['cai_id']}"> <a class="subtract" cai_id="{$list_value['cai_id']}"></a>
                        <input type="text" class="quantity" id="cai_num_{$list_value['cai_id']}" num="{$list_value['cai_num']}" name="cai_num[{$list_value['cai_id']}]" value="0" maxlength="3" />
                        <a class="add" cai_id="{$list_value['cai_id']}"></a></li>
                    <li class="order"><a href="{:U('/cai/'.$list_value['cai_id'])}">查看菜单</a></li>
                </ul>
            </volist>
        </div>
        <div class="method">
            <button class="r" type="submit" >查看购物车</button>
        </div>
        <div class="clear"></div>
    </form>
    <div>
</section>
<script type="text/javascript">
    var cai_action = {
        "change":function(cai_id,num){
            var most =  parseInt($("#cai_num_"+cai_id).attr("num"));
            if (num > most){
                num = most;
            }
            if (num < 0){
                num = 0;
            }
            $("#cai_num_"+cai_id).val(num);
        }
    }
    $(document).ready(function(){
        $(".cai_type_select").click(function (){
            $(".cai_type_select").removeClass("active");
            $(this).addClass("active");
            $(".cai_type_list").hide();
            $(".cai_type_"+$(this).attr("type_id")).show();
        })
        $(".cai_type_select:eq(0)").click();
        $(".subtract,.add").click(function(){
            if ("add" === $(this).attr("class")){
                var now_value = parseInt($("#cai_num_"+$(this).attr("cai_id")).val());
                now_value++;
                cai_action.change($(this).attr("cai_id"),now_value);
            }else{
                var now_value = parseInt($("#cai_num_"+$(this).attr("cai_id")).val());
                now_value--;
                cai_action.change($(this).attr("cai_id"),now_value);
            }
        });
        $("#form").submit(function(){
            var num = 0;
            $("input.quantity").each(function(){
                num += parseInt($(this).val());
            });
            if (num == 0){
                alert("请选择需要下单的商品");
                return false;
            }
        });
    });

</script>